const Router = require('koa-router')

const { validator, checkIdChild, checkId, checkIdfather, checkDuplicateCategory } = require('../middleware/category.middleware')
const { getCategory, addCategory, deleteCategory, updateCategory, searchCategory } = require('../controller/category.controller')

const router = new Router({ prefix: '/category' })

// 添加商品分类
router.post('/addCategory', validator({
    category_name: { type: 'string', required: true },
    parent_category_id: { type: 'number', required: false },
    status: { type: 'boolean', required: false }
}), checkDuplicateCategory, checkIdfather, addCategory)

// 删除商品分类
router.delete('/:id', checkIdChild, deleteCategory)

// 获取商品分类
router.get('/getCategory', getCategory)

// 修改商品分类
router.post('/updateCategory', validator({
    id: { type: 'number', required: true },
    category_name: { type: 'string', required: true },
    status: { type: 'boolean', required: true }
}), checkDuplicateCategory, checkId, updateCategory)

// 查询商品分类
router.post('/searchCategory', validator({ category_name: { type: 'string', required: true } }), searchCategory)

module.exports = router